package com.game.modular.gameCore.match.service.impl;

import com.game.modular.gameCore.match.domain.CoreMatchEndExp;
import lombok.extern.slf4j.Slf4j;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.game.modular.gameCore.match.mapper.CoreMatchEndIntegralMapper;
import com.game.modular.gameCore.match.domain.CoreMatchEndIntegral;
import com.game.modular.gameCore.match.service.CoreMatchEndIntegralService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;


/**
 * (CoreMatchEndIntegral)表服务实现类
 *
 * @author cxy
 * @since 2024-09-10 11:53:12
 */
@Slf4j
@Service
public class CoreMatchEndIntegralServiceImpl extends ServiceImpl<CoreMatchEndIntegralMapper, CoreMatchEndIntegral> implements CoreMatchEndIntegralService {
    Cache<String, Map<Long,CoreMatchEndIntegral>> cache = Caffeine.newBuilder().build();
    
    @PostConstruct
    private void init() {
        CoreMatchEndIntegral initSelect = CoreMatchEndIntegral.builder().build();
        List<CoreMatchEndIntegral> list = this.list(new LambdaQueryWrapper<>(initSelect));
        log.info("开始缓存数据,数据量为:{}",list.size());
        cache.put(
                "coreMatchEndIntegral", 
                list.stream().collect(Collectors.toMap(CoreMatchEndIntegral::getId, CoreMatchEndIntegral::init))
        );
        log.info("数据缓存完成");
    }

    public List<CoreMatchEndIntegral> listCache() {
        Map<Long,CoreMatchEndIntegral> values = cache.getIfPresent("coreMatchEndIntegral");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return new ArrayList<>(values.values());
    }     
    
    public Map<Long,CoreMatchEndIntegral> mapCache() {
        Map<Long,CoreMatchEndIntegral> values = cache.getIfPresent("coreMatchEndIntegral");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values;
    }   
    
    public CoreMatchEndIntegral getCache(Long id) {
        Map<Long,CoreMatchEndIntegral> values = cache.getIfPresent("coreMatchEndIntegral");
        if (Objects.isNull(values)) {
            throw new RuntimeException("数据为空");
        }
        return values.get(id);
    }

    /**
     * 按类型获取结算奖励
     * @param type
     * @return
     */
    public List<CoreMatchEndIntegral> listCache(int type) {
        return listCache().stream().filter(item -> item.getType() == type).collect(Collectors.toList());
    }
}
